package com.arcticcoding.telemetrie;

import java.io.IOException;
import java.net.InetAddress;

import com.arcticcoding.telemetrie.comm.CameraCommand;
import com.arcticcoding.telemetrie.comm.TelemetrieComm;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;

public class TelemetrieClientPC {
	private Client client;
	
	public TelemetrieClientPC() {
		client = new Client(TelemetrieValues.OBJECTBUFFERSIZE, 
				TelemetrieValues.OBJECTBUFFERSIZE);
		client.start();
		
		Kryo kryo;
		kryo = client.getKryo();
		
		for(@SuppressWarnings("rawtypes") Class c: TelemetrieValues.KRYO_CLASSES) {
			kryo.register(c);
		}
	}
	
	public void addClientListener(TelemetrieClientListener listener) {
		client.addListener(listener);
	}
	
	public void connect(final String address, 
			final int tcpPort, final int udpPort) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					client.connect(5000, InetAddress.getByName(address),
							tcpPort, udpPort);
				} catch(IOException e) {
					e.printStackTrace();
				}
			}
			
		}, "Client connector").start();
	}
	
	private void sendCommand(final TelemetrieComm command) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				client.sendTCP(command);
			}
			
		}, "CommandSender").start();
	}
	
	public void sendLedOn() {
		sendCommand(new CameraCommand(CameraCommand.CameraCommandType.LED_ON));
	}
	
	public void sendLedOff() {
		sendCommand(new CameraCommand(CameraCommand.CameraCommandType.LED_OFF));
	}
	
	public void sendStartStreaming() {
		sendCommand(new CameraCommand(CameraCommand.CameraCommandType.START_STREAMING));
	}
	
	public void sendStopStreaming() {
		sendCommand(new CameraCommand(CameraCommand.CameraCommandType.STOP_STREAMING));
	}
}
